﻿using System.Collections.Generic;
using Engine;
using Engine.Graphics;

namespace Game
{
    public class 苹果木头 : AlphaTestCubeBlock
    {
        public const int Index = 303;

        public Texture2D m_texture;

        public override void Initialize()
        {
            m_texture = ContentManager.Get<Texture2D>("Textures/AppleTree/applewood", (string)null);
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {

            BlocksManager.DrawCubeBlock(primitivesRenderer, value, new Vector3(size), ref matrix, color, color, environmentData, m_texture);
        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {

            generator.GenerateCubeVertices((Block)(object)this, value, x, y, z, Color.White, geometry.GetGeometry(m_texture).OpaqueSubsetsByFace);
        }

        public override int GetTextureSlotCount(int value)
        {
            return 3;
        }

        public override BlockDebrisParticleSystem CreateDebrisParticleSystem(SubsystemTerrain subsystemTerrain, Vector3 position, int value, float strength)
        {

            return new BlockDebrisParticleSystem(subsystemTerrain, position, 1f * strength, ((Block)this).DestructionDebrisScale, Color.White, ((Block)this).DefaultTextureSlot);
        }

        public override int GetFaceTextureSlot(int face, int value)
        {
            int cutFace = GetCutFace(Terrain.ExtractData(value));
            if (cutFace == face || CellFace.OppositeFace(cutFace) == face)
            {
                return 1;
            }
            return 0;
        }

        public static int GetCutFace(int data)
        {
            data &= 3; // 限制 data 的值在 0 到 3 之间  

            switch (data)
            {
                case 0:
                    return 4;
                case 1:
                    return 0;
                default:
                    return 1; // 对于 2 或 3 的情况，都返回 1  
            }
        }

        public override BlockPlacementData GetPlacementValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, TerrainRaycastResult raycastResult)
        {

            Matrix val = Matrix.CreateFromQuaternion(componentMiner.ComponentCreature.ComponentCreatureModel.EyeRotation);

            Vector3 forward = val.Forward;
            float num = float.NegativeInfinity;
            int cutFace = 0;
            for (int i = 0; i < 6; i++)
            {
                float num2 = Vector3.Dot(CellFace.FaceToVector3(i), forward);
                if (num2 > num)
                {
                    num = num2;
                    cutFace = i;
                }
            }
            BlockPlacementData result = default(BlockPlacementData);
            result.Value = Terrain.MakeBlockValue(((Block)this).BlockIndex, 0, SetCutFace(0, cutFace));
            result.CellFace = raycastResult.CellFace;
            return result;
        }

        public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris)
        {

            int data = Terrain.ExtractData(oldValue);
            data = SetCutFace(data, 4);
            dropValues.Add(new BlockDropValue
            {
                Value = Terrain.MakeBlockValue(((Block)this).BlockIndex, 0, data),
                Count = 1
            });
            showDebris = true;
        }

        public static int SetCutFace(int data, int cutFace)
        {
            data &= -4;
            switch (cutFace)
            {
                case 0:
                case 2:
                    return data | 1;
                case 1:
                case 3:
                    return data | 2;
                default:
                    return data;
            }
        }
    }
}
